"""
@Filename       : analysis_kg.py
@Create Time    : 2022/3/24 20:28
@Author         : Rylynn
@Description    : 

"""
import json
import random
import matplotlib.pyplot as plt
from scipy.stats import stats

dataset = 'dblp_new'


def kg_to_set(kg):
    entity_set = set()

    if type(kg) is dict:
        for h, rt in kg.items():
            entity_set.add(h)
            for r, t in rt:
                entity_set.add(t)
        return entity_set
    elif type(kg) is list:
        for h, r, t in kg:
            entity_set.add(h)
            entity_set.add(t)
        return entity_set


content_dict = json.load(open('../../../data/{}/content.json'.format(dataset), 'r'))

user_dict = {}
with open('../../../data/{}/cascade.txt'.format(dataset)) as f:
    for line in f.readlines():
        cu = line.strip().split(' ')
        cid = cu[0]
        users = cu[1:]
        for ut in users:
            u = ut.split(',')[0]
            if user_dict.get(u):
                user_dict[u].append(cid)
            else:
                user_dict[u] = [cid]

user_entity_set = {}
for u, cid_list in user_dict.items():
    user_entity_set[u] = set()
    for cid in cid_list:
        user_entity_set[u] = user_entity_set[u].union(kg_to_set(content_dict[cid]))

positive_score_list = []
negative_score_list = []
positive_plot_list = []
negative_plot_list = []
with open('../../../data/{}/cascadetest.txt'.format(dataset)) as f:
    total_num = 0
    p005_num = 0
    p001_num = 0
    for line in f.readlines():
        cu = line.strip().split(' ')
        cid = cu[0]
        users = cu[1:]
        ckg_set = kg_to_set(content_dict[cid])
        if len(ckg_set) == 0 or len(users) < 20:
            continue
        positive_similarity_list = []
        for ut in users:
            u = ut.split(',')[0]
            if not user_entity_set.get(u) or len(ckg_set) == 0:
                similarity = 0
            else:
                ukg_set = user_entity_set[u]
                similarity = len(ckg_set.intersection(ukg_set)) / len(ckg_set)
            positive_plot_list.append(similarity)
            positive_similarity_list.append(similarity)

        negative_similarity_list = []
        neg_users = random.sample(set(user_entity_set.keys()).difference(set(users)), len(positive_similarity_list))
        for nu in neg_users:
            if not user_entity_set.get(nu) or len(ckg_set) == 0:
                similarity = 0
            else:
                ukg_set = user_entity_set[nu]
                similarity = len(ckg_set.intersection(ukg_set)) / len(ckg_set)
            negative_plot_list.append(similarity)
            negative_similarity_list.append(similarity)
        negative_similarity = sum(negative_similarity_list) / len(negative_similarity_list)
        positive_similarity = sum(positive_similarity_list) / len(positive_similarity_list)
        try:
            res = stats.mannwhitneyu(positive_similarity_list, negative_similarity_list)
        except Exception as e:
            continue
        print('pos', positive_similarity)
        print('neg', negative_similarity)
        print('significance', res)
        if res[1] < 0.05:
            p005_num += 1
        if res[1] < 0.01:
            p001_num += 1
        total_num += 1
        positive_score_list.append(positive_similarity)
        negative_score_list.append(negative_similarity)
        # print('pos:', positive_similarity)
        # print('neg:', negative_similarity)
print("Significance Rate: p = 0.05: ", p005_num / total_num)
print("Significance Rate: p = 0.01: ", p001_num / total_num)

# print(np.array(positive_score_list).mean())
# print(np.array(negative_score_list).mean())
# print(np.array(positive_score_list).mean() / np.array(negative_score_list).mean())
fig = plt.figure()
plt.tight_layout()
fig.suptitle(dataset)
f1 = fig.add_sub_plot(1, 2, 1)

f1.set_xlabel('Jarcard similarity')
f1.set_ylabel("Percentage of user (%)")
f1.set_title('Activated User')
f1.hist(positive_plot_list, weights=[1. / len(positive_plot_list)] * len(positive_plot_list))

f2 = fig.add_sub_plot(1, 2, 2)
f2.set_xlabel('Jaccard similarity')
f2.set_ylabel("Number of users")
f2.set_title('Inactivated User')
plt.hist(negative_plot_list, weights=[1. / len(negative_plot_list)] * len(negative_plot_list))
plt.savefig('../../../fig/{}_kg_analysis.png'.format(dataset))

plt.show()

with open('../../../data/{}/cascadetest.txt'.format(dataset)) as f:
    cu = line.strip().split(' ')
    cid = cu[0]
    users = cu[1:]
    ckg_set = kg_to_set(content_dict[cid])
    positive_similarity_list = []
    for ut in users:
        u = ut.split(',')[0]
        if not user_entity_set.get(u) or len(ckg_set) == 0:
            similarity = 0
        else:
            ukg_set = user_entity_set[u]
            similarity = len(ckg_set.intersection(ukg_set)) / len(ckg_set)
        positive_similarity_list.append(similarity)




